package top.xxyu.quartz;

import org.springframework.beans.factory.annotation.Autowired;
import top.xxyu.quartz.service.AppQuartzService;

//@RestController
public class JobController {

    @Autowired
    private JobUtil jobUtil;
    @Autowired
    private AppQuartzService appQuartzService;

/*
    //添加一个job
    @RequestMapping(value = "/addJob", method = RequestMethod.POST)
    public Msg addjob(@RequestBody AppQuartz appQuartz) throws Exception {
        appQuartzService.insertAppQuartzSer(appQuartz);
        Msg msg = new Msg("200", "success pauseJob");
        msg.setResult(jobUtil.addJob(appQuartz));
        return msg;
    }

    //暂停job
    @RequestMapping(value = "/pauseJob", method = RequestMethod.POST)
    public Msg pausejob(@RequestBody Integer[] quartzIds) throws Exception {
        AppQuartz appQuartz = null;
        if (quartzIds.length > 0) {
            for (Integer quartzId : quartzIds) {
                appQuartz = appQuartzService.selectAppQuartzByIdSer(quartzId);
                jobUtil.pauseJob(appQuartz.getJobName(), appQuartz.getJobGroup());
            }
            return new Msg("200", "success pauseJob");
        } else {
            return new Msg("404", "fail pauseJob");
        }
    }

    //恢复job
    @RequestMapping(value = "/resumeJob", method = RequestMethod.POST)
    public Msg resumejob(@RequestBody Integer[] quartzIds) throws Exception {
        AppQuartz appQuartz = null;
        if (quartzIds.length > 0) {
            for (Integer quartzId : quartzIds) {
                appQuartz = appQuartzService.selectAppQuartzByIdSer(quartzId);
                jobUtil.resumeJob(appQuartz.getJobName(), appQuartz.getJobGroup());
            }
            return new Msg("200", "success resumeJob");
        } else {
            return new Msg("404", "fail resumeJob");
        }
    }


    //删除job
    @RequestMapping(value = "/deletJob", method = RequestMethod.POST)
    public Msg deletjob(@RequestBody Integer[] quartzIds) throws Exception {
        AppQuartz appQuartz = null;
        for (Integer quartzId : quartzIds) {
            appQuartz = appQuartzService.selectAppQuartzByIdSer(quartzId);
            String ret = jobUtil.deleteJob(appQuartz);
            if ("success".equals(ret)) {
                appQuartzService.deleteAppQuartzByIdSer(quartzId);
            }
        }
        return new Msg("200", "success deleteJob");
    }

    //修改
    @RequestMapping(value = "/updateJob", method = RequestMethod.POST)
    public Msg modifyJob(@RequestBody AppQuartz appQuartz) throws Exception {
        String ret = jobUtil.modifyJob(appQuartz);
        if ("success".equals(ret)) {
            appQuartzService.updateAppQuartzSer(appQuartz);
            return new Msg("200", "success updateJob", ret);
        } else {
            return new Msg("404", "fail updateJob", ret);
        }
    }

    //暂停所有
    @RequestMapping(value = "/pauseAll", method = RequestMethod.GET)
    public Msg pauseAllJob() throws Exception {
        jobUtil.pauseAllJob();
        return new Msg("200", "success pauseAll");
    }

    //恢复所有
    @RequestMapping(value = "/repauseAll", method = RequestMethod.GET)
    public Msg repauseAllJob() throws Exception {
        jobUtil.resumeAllJob();
        return new Msg("200", "success repauseAll");
    }*/
}
